package com.weimob.mcloud.wechatwork.customer.third;

import com.alibaba.fastjson.JSON;
import com.weimob.mcloud.wechatwork.customer.service.user.exception.BizException;
import com.weimob.mengdian.pic.sdk.PicServerClient;
import com.weimob.mengdian.pic.sdk.exception.PicServerException;
import com.weimob.mengdian.pic.sdk.model.*;
import com.weimob.mengdian.pic.sdk.request.UploadRequest;
import com.weimob.zipkin.ZipkinContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.io.File;
import java.io.InputStream;

/**
 * @author yuzhaoyang
 * @desc 媒体中心代理类
 * @date 2021-01-27 23:41
 */
@Component
@Slf4j
public class PicServerProxy {

    @Autowired
    private PicServerClient picServerClient;

    /**
     * 文件上传并获取URL
     *
     * @param inputStream
     * @param fileName
     * @return
     */
    public UploadRes getUploadRes(InputStream inputStream, String fileName) {
        UploadRequest uploadRequest = UploadRequest.build().uploadFile(inputStream, fileName);
        return this.doUpload(uploadRequest);
    }

    /**
     * 上传到媒体中心
     *
     * @param uploadRequest
     * @return
     */
    private UploadRes doUpload(UploadRequest uploadRequest) {
        BaseResponse<UploadRes> resBaseResponse;
        try {
            resBaseResponse = picServerClient.upload(uploadRequest);
        } catch (PicServerException e) {
            log.error("媒体中新上传文件失败，cause by:{}", e.getErrorCode(), e);
            throw new BizException(Long.valueOf(e.getErrorCode().getCode()), e.getErrorCode().getMsg());
        }
        Assert.notNull(resBaseResponse, "resBaseResponse must not null");
        if (resBaseResponse.getCode() == null || resBaseResponse.getCode() != 0) {
            log.error("媒体中新上传文件失败，result:{}", JSON.toJSONString(resBaseResponse));
            throw new BizException(Long.valueOf(resBaseResponse.getCode()), resBaseResponse.getMsg());
        } else {
            return resBaseResponse.getData();
        }
    }

    public InputStream download(String url){
        try {
            DownloadFileResponse downloadFileResponse = picServerClient.download(url);
            log.info("PicServerProxy download url:{},ticket:{}",url, ZipkinContext.getContext().getGlobalTicket());
            return downloadFileResponse.getInputStream();
        } catch (Exception e) {
            log.info("PicServerProxy error url:{},e:{},ticket:{}",url,e, ZipkinContext.getContext().getGlobalTicket());
            throw new BizException(123123L,"1232132");
        }
    }


    /**
     * 文件上传并获取URL
     *
     * @param file
     * @param fileName
     * @return
     */
    public UploadRes getUploadRes(File file, String fileName) {
        UploadRequest uploadRequest = UploadRequest.build().uploadFile(file)
                .withFileName(fileName);
        return this.doUpload(uploadRequest);
    }

    /**
     * 生成二维码
     *
     * @param qrCodeRequest
     */
    public UploadImageRes createQrcode(CreateQrCodeRequest qrCodeRequest) {
        BaseResponse<UploadImageRes> resBaseResponse;
        try {
            resBaseResponse = picServerClient.createQrCode(qrCodeRequest);
        }
        catch (PicServerException e) {
            log.error("媒体中生成二维码失败，cause by:{}", e.getErrorCode(), e);
            throw new BizException(Long.valueOf(e.getErrorCode().getCode()), e.getErrorCode().getMsg());
        }
        Assert.notNull(resBaseResponse, "resBaseResponse must not null");
        if (resBaseResponse.getCode() == null || resBaseResponse.getCode() != 0) {
            log.error("媒体中生成二维码失败，result:{}", JSON.toJSONString(resBaseResponse));
            throw new BizException(Long.valueOf(resBaseResponse.getCode()), resBaseResponse.getMsg());
        }
        else {
            return resBaseResponse.getData();
        }
    }

}
